package net.zjitc.interceptor;

import net.zjitc.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthorizationInterceptor implements HandlerInterceptor {
    /*不对以下请求拦截*/
    private static final String[] IGNORE_URL={"/login","/register","/jsonLogin"};
    /*拦截处理，在Controller调用之前调用*/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String servletPath = request.getServletPath();
      /*  for(String s:IGNORE_URL){
            if (servletPath.contains(s)){
                return true;
            }
        }*/
        User user = (User) request.getSession().getAttribute("user");
        if (user!= null){
            return true;
        }else {
            request.getRequestDispatcher("/login.do").forward(request,response);
        }
        return false;
    }

    /*此方法在Controller调用之后，preHandle返回为true时调用，
    * 此方法可对modelAndView进行操作
    * */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /*整个请求完成后，调用此方法，用于清理资源*/
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
